using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using Newtonsoft.Json.Linq;

public class SimpleThemeComparison : Form
{
    private Panel defaultPanel;
    private Panel lightPanel;
    private Panel darkPanel;
    
    public SimpleThemeComparison()
    {
        InitializeComponent();
        DisplayThemeColors();
    }
    
    private void InitializeComponent()
    {
        this.Text = "主题颜色对比";
        this.Size = new Size(800, 600);
        this.StartPosition = FormStartPosition.CenterScreen;
        
        // 创建面板用于显示主题
        defaultPanel = new Panel();
        defaultPanel.Location = new Point(10, 10);
        defaultPanel.Size = new Size(250, 500);
        defaultPanel.BorderStyle = BorderStyle.FixedSingle;
        
        lightPanel = new Panel();
        lightPanel.Location = new Point(270, 10);
        lightPanel.Size = new Size(250, 500);
        lightPanel.BorderStyle = BorderStyle.FixedSingle;
        
        darkPanel = new Panel();
        darkPanel.Location = new Point(530, 10);
        darkPanel.Size = new Size(250, 500);
        darkPanel.BorderStyle = BorderStyle.FixedSingle;
        
        // 添加面板到窗体
        this.Controls.Add(defaultPanel);
        this.Controls.Add(lightPanel);
        this.Controls.Add(darkPanel);
    }
    
    private void DisplayThemeColors()
    {
        try
        {
            string basePath = Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "MouseKeyboardOperationSimulator", "themes");
            
            // 加载默认主题
            string defaultThemePath = Path.Combine(basePath, "theme.json");
            if (File.Exists(defaultThemePath))
            {
                string defaultThemeContent = File.ReadAllText(defaultThemePath);
                DisplayThemeFromJson(defaultPanel, defaultThemeContent, "默认主题");
            }
            else
            {
                DisplayErrorMessage(defaultPanel, "默认主题", "未找到主题文件");
            }
            
            // 加载明亮主题
            string lightThemePath = Path.Combine(basePath, "light_theme.json");
            if (File.Exists(lightThemePath))
            {
                string lightThemeContent = File.ReadAllText(lightThemePath);
                DisplayThemeFromJson(lightPanel, lightThemeContent, "明亮主题");
            }
            else
            {
                DisplayErrorMessage(lightPanel, "明亮主题", "未找到主题文件");
            }
            
            // 加载暗色主题
            string darkThemePath = Path.Combine(basePath, "dark_theme.json");
            if (File.Exists(darkThemePath))
            {
                string darkThemeContent = File.ReadAllText(darkThemePath);
                DisplayThemeFromJson(darkPanel, darkThemeContent, "暗色主题");
            }
            else
            {
                DisplayErrorMessage(darkPanel, "暗色主题", "未找到主题文件");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show($"加载主题时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    
    private void DisplayThemeFromJson(Panel panel, string jsonContent, string themeName)
    {
        try
        {
            JObject theme = JObject.Parse(jsonContent);
            
            // 添加主题名称标签
            Label nameLabel = new Label();
            nameLabel.Text = themeName;
            nameLabel.Font = new Font(nameLabel.Font, FontStyle.Bold);
            nameLabel.Location = new Point(10, 10);
            nameLabel.Size = new Size(200, 20);
            nameLabel.BackColor = GetColorFromHex("#FFFFFF"); // 默认白色背景
            nameLabel.ForeColor = GetColorFromHex("#000000"); // 默认黑色文字
            panel.Controls.Add(nameLabel);
            
            // 获取颜色定义
            var colors = theme["colors"];
            if (colors != null)
            {
                int yPos = 40;
                foreach (var color in colors)
                {
                    string colorName = color.Key;
                    string colorValue = color.Value.ToString();
                    
                    try
                    {
                        Color bgColor = GetColorFromHex(colorValue);
                        
                        Panel colorPanel = new Panel();
                        colorPanel.Location = new Point(10, yPos);
                        colorPanel.Size = new Size(200, 30);
                        colorPanel.BorderStyle = BorderStyle.FixedSingle;
                        colorPanel.BackColor = bgColor;
                        
                        Label colorLabel = new Label();
                        colorLabel.Text = $"{colorName}: {colorValue}";
                        colorLabel.Location = new Point(10, yPos + 35);
                        colorLabel.Size = new Size(200, 15);
                        colorLabel.BackColor = GetColorFromHex("#FFFFFF"); // 白色背景
                        colorLabel.ForeColor = GetColorFromHex("#000000"); // 黑色文字
                        
                        panel.Controls.Add(colorPanel);
                        panel.Controls.Add(colorLabel);
                        
                        yPos += 60;
                    }
                    catch (Exception ex)
                    {
                        Label errorLabel = new Label();
                        errorLabel.Text = $"{colorName} 解析错误: {ex.Message}";
                        errorLabel.Location = new Point(10, yPos);
                        errorLabel.Size = new Size(200, 30);
                        panel.Controls.Add(errorLabel);
                        yPos += 40;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            DisplayErrorMessage(panel, themeName, $"解析主题文件时出错: {ex.Message}");
        }
    }
    
    private void DisplayErrorMessage(Panel panel, string themeName, string errorMessage)
    {
        Label errorLabel = new Label();
        errorLabel.Text = errorMessage;
        errorLabel.Location = new Point(10, 40);
        errorLabel.Size = new Size(200, 60);
        errorLabel.ForeColor = Color.Red;
        panel.Controls.Add(errorLabel);
    }
    
    private Color GetColorFromHex(string hexColor)
    {
        try
        {
            // 移除可能存在的 # 符号
            if (hexColor.StartsWith("#"))
                hexColor = hexColor.Substring(1);
            
            // 转换十六进制颜色值为Color对象
            if (hexColor.Length == 6)
            {
                int r = Convert.ToInt32(hexColor.Substring(0, 2), 16);
                int g = Convert.ToInt32(hexColor.Substring(2, 2), 16);
                int b = Convert.ToInt32(hexColor.Substring(4, 2), 16);
                return Color.FromArgb(r, g, b);
            }
            else if (hexColor.Length == 8)
            {
                // 包含Alpha通道
                int a = Convert.ToInt32(hexColor.Substring(0, 2), 16);
                int r = Convert.ToInt32(hexColor.Substring(2, 2), 16);
                int g = Convert.ToInt32(hexColor.Substring(4, 2), 16);
                int b = Convert.ToInt32(hexColor.Substring(6, 2), 16);
                return Color.FromArgb(a, r, g, b);
            }
        }
        catch
        {
            // 如果转换失败，返回默认颜色
            return Color.Gray;
        }
        
        return Color.Gray;
    }
    
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new SimpleThemeComparison());
    }
}